package com.teamTPZoo.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.AutoShape;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.Table;
import org.apache.poi.hslf.usermodel.SlideShow;

//import com.posdata.glue.context.PosContext;
//import com.posdata.glue.dao.vo.PosRow;
//import com.posdata.glue.dao.vo.PosRowSet;

/**
 * PPT 생성 유틸리티
 * @author 최정국
 */
public class PPTUtil {
/*

	public static String ROOT_PACKAGE = "template";
	private static String EXT = ".ppt";

	private Logger logger = Logger.getLogger(this.getClass());

	private SlideShow slideShow = null;
	private int size = 0;

	public static PPTUtil newInstanceFromResource(String templateId, int size) throws IOException {
		return new PPTUtil(ROOT_PACKAGE, templateId, size);
	}
	
	public static PPTUtil newInstanceFromFile(String filename, int size) throws IOException {
		return new PPTUtil(filename, size);
	}

	public PPTUtil(String packageName, String templateId, int size) throws IOException {
		if (logger.isDebugEnabled()) logger.debug("TEMPLATE [" + packageName + File.separator + templateId + EXT + "][" + size + "]");
		slideShow = new SlideShow(new HSLFSlideShow(Thread.currentThread().getContextClassLoader().getResourceAsStream(packageName + File.separator + templateId + EXT)));
		this.size = size;
	}
	
	public PPTUtil(String filename, int size) throws IOException {
		if (logger.isDebugEnabled()) logger.debug("PATH [" + filename + "][" + size + "]");
		slideShow = new SlideShow(new HSLFSlideShow(new FileInputStream(filename)));
		this.size = size;
	}

	public SlideShow getSlideShow() {
		return slideShow;
	}

	public void process(PosContext ctx, int cnt) throws Exception {
		if (logger.isDebugEnabled()) logger.debug("PROCESSING START");
		for (int i= size * cnt; i< size * cnt + size; i++) {
			if (logger.isDebugEnabled()) logger.debug("PROCESSING [" + i + "]");
			Shape[] shapes = slideShow.getSlides()[i].getShapes();
			for (int j=0; j<shapes.length; j++) {
				traverse(ctx, shapes[j]);
			}
		}
		if (logger.isDebugEnabled()) logger.debug("PROCESSING END");
	}

	private void traverse(PosContext ctx, Shape shape) throws Exception {
		if (shape instanceof AutoShape) {
			replace(ctx, (AutoShape)shape);
		}
		else if (shape instanceof Table) {
			Table table = (Table)shape;
			Shape[] shapes = table.getShapes();
			for (int i=0; i<shapes.length; i++) {
				traverse(ctx, shapes[i]);
			}
		}
	}

	private void replace(PosContext ctx, AutoShape shape) throws Exception {
		// AutoShape 변수 추출 ${...}
		StringTokenizer token1 = new StringTokenizer(shape.getText(), "${");

		StringBuffer buffer = new StringBuffer();

		while (token1.hasMoreElements()) {
			String token = token1.nextToken();
			int length = token.lastIndexOf("}");
			if (length == -1) {
				buffer.append(token);
				continue;
			}
			else {			
				String key = token.substring(0, length);

				// result.attr
				StringTokenizer token2 = new StringTokenizer(key, ".");
				if (token2.countTokens() == 2) {
					String resultKey = token2.nextToken();
					String attrKey = token2.nextToken();

					PosRowSet rowSet = ctx.getRowSet(resultKey);
					if (rowSet != null && rowSet.count() != 0) {
						rowSet.reset();

						int count = 0;
						while (rowSet.hasNext()) {
							PosRow row = rowSet.next();
							String attr = S07020Util.changeFormat(row.getAttribute(attrKey), null, "", false).trim();
							if (count != 0) buffer.append("\n");
							buffer.append(attr);							
							count++;
						}
					}
				}
				buffer.append(token.substring(length+1));
			}
		}
		shape.setText(buffer.toString());
	}
*/	
}